//
//  FlowSubtitles.m
//  FirstMuisc
//
//  Created by 元虎的mac on 15/9/4.
//  Copyright (c) 2015年 杨元虎. All rights reserved.
//

#import "FlowSubtitles.h"


@implementation FlowSubtitles


- (void)dealloc
{
    [_time invalidate];
}

- (instancetype)initWithFrame:(CGRect)frame str:(NSString *)Str Font:(NSInteger)Font
{
    self = [super initWithFrame:frame];
    if (self) {//此处计算的是宽度
        self.userInteractionEnabled = NO;
        [self returnStringHeight:Str Wdth:frame.size.height Font:Font];
        _title = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, _wdth>frame.size.width?_wdth:frame.size.width, frame.size.height)];
        
        if (!Str) {
            _title.text = @"还没有标题";
        }else{
            _title.text = Str;
        }
        
        _title.textColor = [UIColor blackColor];
        _title.numberOfLines = 0;
        _title.textAlignment = NSTextAlignmentLeft;
        _title.font = [UIFont systemFontOfSize:Font];
        [self addSubview:_title];
        self.contentSize = CGSizeMake(_title.width, frame.size.height);
        //创建定时器
        self.time = [NSTimer scheduledTimerWithTimeInterval:0.03 target:self selector:@selector(overtime:) userInfo:nil repeats:YES];
        self.tmeer = 0;
        NSDate *date = [NSDate date];
        NSLog(@"%@",date);
        NSDate *FireDate = [NSDate dateWithTimeInterval:3 sinceDate:date];
        [self.time setFireDate:FireDate];
    }
    return self;
}

- (void)SetPlaySpend:(CGFloat)spend{
    [self.time invalidate];
    self.time=nil;
    self.time = [NSTimer scheduledTimerWithTimeInterval:spend target:self selector:@selector(overtime:) userInfo:nil repeats:YES];
}

- (CGFloat)returnStringHeight:(NSString *)string Wdth:(CGFloat)Wdth Font:(NSInteger)font{
    CGRect frame = [string boundingRectWithSize:CGSizeMake(10000, Wdth) options:(NSStringDrawingUsesLineFragmentOrigin) attributes:[NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:font] forKey:NSFontAttributeName] context:nil];
    self.wdth = frame.size.width;
    return frame.size.width;
}
- (void)overtime:(NSTimer *)time{
    //定时器每隔一段时间调用这个方法
    //我们设置一个数来进行增加和减少 并控制滚动的偏移距离   
    self.tmeer++;
    if (_wdth>self.width) {
        self.contentOffset = CGPointMake(self.tmeer, 0);
        if (self.tmeer>self.wdth) {//如果偏移距离超过需要的宽的我们把它重新开始滚动
            self.tmeer = -self.width;
            self.contentOffset = CGPointMake(self.tmeer, 0);
        }
    }
    //self.contentOffset = CGPointMake(self.tmeer, 0);
    
}
-(void)SetTitleText:(NSString *)Text WithFont:(NSInteger)Font{

    self.title.text = Text;
    [self returnStringHeight:Text Wdth:self.frame.size.height Font:Font];
    _title.frame = CGRectMake(0, 0, _wdth>self.frame.size.width?_wdth:self.frame.size.width, self.frame.size.height);
    self.contentSize = CGSizeMake(_title.width, self.frame.size.height);
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
